<?php 
namespace Material\Controller;

use Admin\Controller\AdminbaseController;
class ContentadminController extends AdminbaseController{
	
	protected $m;
	protected $imgLogic;
	protected $fileLogic;
	protected $standard;
	protected $Contentsstandard;
	protected $ret;//getsulie函数的全局赋值
	protected $ContentsLogic;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('Contents');
		$this->imgLogic = D('MaterialImages','Logic');
		$this->fileLogic = D('MaterialFiles','Logic');
		$this->standard = D('Standard');
		$this->ContentsLogic = D('Contents','Logic');
		$this->Contentsstandard = D('ContentsStandard');
	}
	
	
	/**
	 * @name 资讯列表
	 * @auth adminleft
	 */
	public function index(){
		$this->cat = I('get.cat','content');
		
		if(!$_GET['status']){
			$_GET['status'] = 0;
		}
		$catsLogic = D('Cats','Logic');
		$this->catsoptions = $catsLogic->_get_select_options_by_groupkey($this->cat,$_GET['cid']);
		
		$this->catids = $catsLogic->_get_cat_ids_string_by_groupkey($this->cat);
		
		
		$this->display();
	}
	
	//规格模型管理列表
	public function lists(){
	
		if(!$_GET['status']){
			$_GET['status'] = 0;
		}
	
		$this->display();
	}
	
	public function edit(){
		if(isset($_GET['id'])){
			$info = $this->m->relation(true)->find($_GET['id']);
				
			//判读规格模型是否被更改,入更改，设模型id为-1
			$standard_id = $info['standard']['standard_id'];
			if($standard_id){
				$guige = $this->standard->where('id = '.$standard_id)->getField('guige');
				if($guige !== $info['standard']['standard_model']){
					$info['standard']['standard_id'] = '-1';
				}
			}
				
			$this->info = $info;
		}
		$cat = I('get.cat','content');//分类
		$catsLogic = D('Cats','Logic');
		$this->catsoptions = $catsLogic->_get_select_options_by_groupkey($cat,$this->info['cid']);
	
		$con['status'] = 1;
		$this->guige = $this->standard->where($con)->order('id desc')->select();
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = Date('Y-m-d H:i:s');
			if($_POST['id']){
				if($this->m->create()){
					$id = $_POST['id'];
					if($this->m->save()){
						$this->imgLogic->save($id,'content',$_POST);
						$this->fileLogic->save($id,'content',$_POST);
						$this->ContentsLogic->_save_standard($id,$_POST);
						$this->success('更新成功');
					}else{
						$this->error('更新失败');
					}
				}
			}else{
				$_POST['create_time'] = Date('Y-m-d H:i:s');
				if($this->m->create()){
					$id = $this->m->add();
					if($id){
						$this->imgLogic->save($id,'content',$_POST);
						$this->fileLogic->save($id,'content',$_POST);
						$this->ContentsLogic->_save_standard($id,$_POST);
						$this->success('添加成功');
					}else{
						$this->error('添加失败');
					}
				}
			}
		}
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('id' => array('in', $id));
		$info = $this->m->where($map)->find();
		if($this->m->where($map)->delete()){
			//删除文章下面的图片
			foreach($id as $i){
				$this->imgLogic->del($i,'content');
				$this->fileLogic->del($i,'content');
				$this->ContentsLogic->_del_standard($i);
			}
			$this->success('删除成功',U('index'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function listorders() {
		$status = parent::_listorders($this->m);
		if ($status) {
			$this->success("排序更新成功！",U('index'));
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	public function changestatus(){
		$data[$this->m->getPk()] = $_REQUEST['id'];
		$data['status'] = $_REQUEST['status'];
		$data['update_time'] = date('Y-m-d H:i:s');
		if($this->m->save($data)){
			$this->success('状态更新成功');
		}else{
			$this->error('状态更新失败');
		}
	}
	
	//添加模板
	public function standard(){
		if(isset($_GET['id'])){
			$info = $this->standard->relation(true)->find($_GET['id']);
			$this->info = $info;
		}
	
		$this->display();
	}
	
	public function standard_post(){
		if($_POST){
			$guige_name = $_POST['guige_name'];
			$guige_cichun = $_POST['guige_cichun'];
			unset($_POST['guige_name']);
			unset($_POST['guige_cichun']);
			$data = $_POST;
			foreach ($guige_name as $key=>$val){
				if(!empty($val) && !empty($guige_cichun[$key])){
					$guige[trim($val)] = trim($guige_cichun[$key]);
				}
			}
			$data['guige'] = json_encode($guige);
			$data['update_time'] = Date('Y-m-d H:i:s');
			if($this->standard->create($data)){
				unset($data['__hash__']);
				if($data['id']){
					if($this->standard->save($data)){
						$this->success('修改成功');
					}else{
						$this->error('修改失败');
					}
				}else{
					$data['create_time'] = $data['update_time'];
					if($this->standard->add($data)){
						$this->success('添加成功');
					}else{
						$this->error('添加失败');
					}
				}
			}else{
				$this->error($this->standard->getError());
			}
		}
	}
	
	//模型状态修改
	public function changestandardstatus(){
		$data[$this->standard->getPk()] = $_REQUEST['id'];
		$data['status'] = $_REQUEST['status'];
		$data['update_time'] = date('Y-m-d H:i:s');
		if($this->standard->save($data)){
			$this->success('状态更新成功');
		}else{
			$this->error('状态更新失败');
		}
	}
	
	//删除模型
	public function delstandard(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('id' => array('in', $id));
		if($this->standard->where($map)->delete()){
			$this->success('删除成功',U('lists'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function autostandard(){
		$contentid = I('post.articleid','0');
		$id = I('post.id','0');
		if(!$contentid) $contentid=0;
		$Contentsstandardinfo = $this->Contentsstandard->where('id = '.$contentid)->find();
		//规格值
		$standard = json_decode($Contentsstandardinfo['standard_content'],true);
		if($id == $Contentsstandardinfo['standard_id'] || $id == '-1'){
			$guige = $Contentsstandardinfo['standard_model'];
		}else{
			$standardinfo = $this->standard->where('id = '.$id)->find();
			$guige = $standardinfo['guige'];
		}
		//$guige = $Contentsstandardinfo['standard_model'];
		// 			$standard = json_decode($Contentsstandardinfo['standard_content'],true);
	
	
		$guige = json_decode($guige);
	
		foreach ($guige as $key=>$val){
			$list[$key] = array_filter(explode(',', $val));
			$str_head .='<th>'.$key.'</th>';
		}
		if($str_head){
			$html = '<table class="table table-border table-bordered table-hover">';
			$html .= '<thead>';
			$html .= '	<tr>'.$str_head;
			$html .= '<th><strong class="red">*</strong>价格</th>';
			$html .= '<th><strong class="red">*</strong>数量</th>';
			$html .= '<th>商品编码</th>';
			$html .= '  </tr>';
		}
		$data = $this->getsulie($list);
		foreach ($data as $dt){
			$html_td = '';
			$arr = explode('_', $dt);
			foreach($arr as $a){
				$html_td .= '<td>'.$a.'</td>';
			}
			$html_c .= '<tr>'.$html_td;
			$html_c .= '<td><input class="input-text" style="border:none;"  datatype="price" name="gg['.$dt.'][price]" value="'.$standard[$dt]['price'].'" type="text"></td>';
			$html_c .= '<td><input class="input-text" style="border:none;"  datatype="n" name="gg['.$dt.'][quantity]" value="'.$standard[$dt]['quantity'].'" type="text"></td>';
			$html_c .= '<td><input class="input-text" style="border:none;"  name="gg['.$dt.'][code]" value="'.$standard[$dt]['code'].'" type="text"></td>';
			$html_c .= '</tr>';
		}
		$html .= $html_c;
		$html .= '</thead></table>';
		// 		dump($html);
		$this->success($html);
		//$this->ajaxReturn($html,'返回成功',1);
	}
	
	//排序组合
	Public function getsulie($list,$content="",$deep="0"){
		$i=0;
		if($deep>count($list)){
			return;
		}
		foreach($list as $k=>$v){
			if($i==$deep){
				foreach($list[$k] as $vv){
					if($content){
						$vv2 = $content.'_'.$vv;
					}else{
						$vv2 = $vv;
					}
					if($deep==count($list)-1){
						$this->ret[] = $vv2;
					}else {
						$this->getsulie($list,$vv2,$deep+1);
					}
				}
				break;
			}
			$i++;
		}
		return $this->ret;
	}
}

?>